package jubaozhu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jubaozhu.domain.Orders;
import jubaozhu.domain.User;
import jubaozhu.extra.base.BaseServiceImpl;
import jubaozhu.extra.pojo.OrderPo;
import jubaozhu.mapper.OrdersMapper;
import jubaozhu.mapper.UserMapper;
import jubaozhu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.utils.IDUtils;

import java.util.Date;

/**
 * The class create by 执笔 on generate.
 */
@Service
@Transactional
public class OrdersServiceImpl extends BaseServiceImpl<Orders> implements OrdersService {


    @Autowired
    public OrdersServiceImpl(OrdersMapper mapper) {
        super(mapper);
    }

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void apply(Orders orders, User user) {
        orders.setAddtime(new Date());
        orders.setUserid(user.getId());
        orders.setStatus("wait");
        orders.setOrderno(IDUtils.getUUID());
        ordersMapper.insertSelective(orders);

        //更新账户
        user.setNumber(user.getNumber() + 1);
        user.setAccount(user.getAccount() + orders.getAccount());
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageInfo<OrderPo> selectPoByExample(Example example, Page page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return new PageInfo<OrderPo>(ordersMapper.selectPoByExample(example),5);
    }
}